<template>
  <div class="custom-layout">
    <!-- 引入导航栏 -->
    <CustomNavbar/>

    <div class="main-content">
      <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">

          <el-form-item label="名称" prop="name">
            <el-input
              v-model="queryParams.name"
              placeholder="请输入名称"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>

          <el-form-item label="付款时间" prop="payTime">
            <el-date-picker clearable
                            v-model="queryParams.payTime"
                            type="date"
                            value-format="yyyy-MM-dd"
                            placeholder="请选择付款时间">
            </el-date-picker>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
              v-hasPermi="['orders:orders:add']"
            >新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="success"
              plain
              icon="el-icon-edit"
              size="mini"
              :disabled="single"
              @click="handleUpdate"
              v-hasPermi="['orders:orders:edit']"
            >修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              plain
              icon="el-icon-delete"
              size="mini"
              :disabled="multiple"
              @click="handleDelete"
              v-hasPermi="['orders:orders:remove']"
            >删除</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="warning"
              plain
              icon="el-icon-download"
              size="mini"
              @click="handleExport"
              v-hasPermi="['orders:orders:export']"
            >导出</el-button>
          </el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="ordersList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="id" align="center" prop="id" v-if="false"/>
          <el-table-column label="分类id" align="center" prop="typeId"v-if="false" />
          <el-table-column label="具体项目id,例如分类为商品则为商品id" align="center" prop="projectId" v-if="false"/>
          <el-table-column label="用户id" align="center" prop="userId" v-if="false"/>
          <el-table-column label="名称" align="center" prop="name" />
          <el-table-column label="原价" align="center" prop="originalPrice" />
          <el-table-column label="付款金额" align="center" prop="account" />
          <el-table-column label="多个商品id" align="center" prop="multiple" v-if="false"/>
          <el-table-column label="订单状态" align="center" prop="status">
            <template slot-scope="scope">
              <span v-if="scope.row.status === 0" style="color:#F56C6C ">未付款</span>
              <span v-else-if="scope.row.status === 1" style="color: #67C23A">已付款</span>
              <span v-else>{{ scope.row.status }}</span>
            </template>
          </el-table-column>
          <el-table-column label="订单处理状态" align="center" prop="payStatus">
            <template slot-scope="scope">
              <span v-if="scope.row.payStatus === 1" style="color:#F56C6C ">未处理</span>
              <span v-else-if="scope.row.payStatus === 0" style="color: #67C23A">已处理</span>
              <span v-else>{{ scope.row.payStatus }}</span>
            </template>
          </el-table-column>
          <el-table-column label="付款时间" align="center" prop="payTime" width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.payTime, '{y}-{m}-{d} {h}:{m}:{s}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['orders:orders:edit']"
              >修改</el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['orders:orders:remove']"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />

        <!-- 添加或修改订单管理对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
          <el-form ref="form" :model="form" :rules="rules" label-width="80px">

            <el-form-item label="分类" prop="typeId">
              <el-select v-model="form.typeId" placeholder="请选择分类">
                <el-option
                  v-for="item in typeOptions"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="具体项目id" prop="projectId">
              <el-input v-model="form.projectId" placeholder="请输入具体项目id,例如分类为商品则为商品id" />
            </el-form-item>
            <el-form-item label="用户id" prop="userId">
              <el-input v-model="form.userId" placeholder="请输入用户id" />
            </el-form-item>
            <el-form-item label="订单名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入项目名称" />
            </el-form-item>
            <el-form-item label="原价" prop="originalPrice">
              <el-input v-model="form.originalPrice" placeholder="请输入原价" />
            </el-form-item>
            <el-form-item label="状态" prop="status">
              <el-select v-model="form.status" placeholder="请选择状态">
                <el-option
                  v-for="item in statusOptions"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="订单处理状态" prop="payStatus">
              <el-select v-model="form.payStatus" placeholder="请选择订单处理状态">
                <el-option
                  v-for="item in payStatusOptions"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="付款金额" prop="account">
              <el-input v-model="form.account" placeholder="请输入付款金额" />
            </el-form-item>
            <el-form-item label="多个商品id" prop="multiple">
              <el-input v-model="form.multiple" placeholder="请输入多个商品id" />
            </el-form-item>
            <el-form-item label="付款时间" prop="payTime">
              <el-date-picker clearable
                              v-model="form.payTime"
                              type="date"
                              value-format="yyyy-MM-dd HH:mm:ss"
                              placeholder="请选择付款时间">
              </el-date-picker>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="submitForm">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
          </div>
        </el-dialog>
      </div>
    </div>
  </div>

</template>

<script>
import { listOrders, getOrders, delOrders, addOrders, updateOrders } from "@/api/orders/orders"
import CustomNavbar from "@/components/CustomNavbar";

export default {
  name: "Orders",
  components: {CustomNavbar},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单管理表格数据
      ordersList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //分类列表
      typeOptions:[
        {
          id:1,
          name:"娱乐项目"
        },
        {
          id:2,
          name:"商品"
        },
        {
          id: 3,
          name: "会员卡"
        },
        {
          id:4,
          name:"套餐"
        }
      ],
      //订单状态数组
      statusOptions:[{
        id:0,
        name:"未付款"
      },{
        id:1,
        name:"已付款"
      }],
      //订单处理状态数组
      payStatusOptions:[{
        id:1,
        name:"未处理"
      },{
        id:0,
        name:"已处理"
      }],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        typeId: null,
        projectId: null,
        userId: null,
        name: null,
        originalPrice: null,
        account: null,
        multiple: null,
        status: null,
        payTime: null,
        payStatus:null,
        createUser: null,
        updateUser: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        typeId: [
          { required: true, message: "分类id不能为空", trigger: "blur" }
        ],
        userId: [
          { required: true, message: "用户id不能为空", trigger: "blur" }
        ],
        name: [
          { required: true, message: "项目名称不能为空", trigger: "blur" }
        ],
        originalPrice: [
          { required: true, message: "原价不能为空", trigger: "blur" }
        ],
        account: [
          { required: true, message: "付款金额不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "订单状态:0未支付1已付款不能为空", trigger: "change" }
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询订单管理列表 */
    getList() {
      this.loading = true
      listOrders(this.queryParams).then(response => {
        this.ordersList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        typeId: null,
        projectId: null,
        userId: null,
        name: null,
        originalPrice: null,
        account: null,
        multiple: null,
        status: null,
        payTime: null,
        payStatus:null,
        createTime: null,
        updateTime: null,
        createUser: null,
        updateUser: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加订单管理"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getOrders(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改订单管理"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateOrders(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addOrders(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除订单管理编号为"' + ids + '"的数据项？').then(function() {
        return delOrders(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('orders/orders/export', {
        ...this.queryParams
      }, `orders_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
<style >
/* 使用与首页一致的配色方案 */
.main-content {
  position: absolute;
  top: 0; /* 强制贴顶 */
  left: 260px; /* 等于导航栏宽度 */
  width: calc(100% - 260px);
  height: 100vh;
  overflow-y: auto;

  background: white;
  z-index: 10; /* 确保在导航栏上方 */
}
:root {
  --primary-100: #8B5FBF;
  --primary-200: #61398F;
  --primary-300: #FFFFFF;
  --accent-100: #D6C6E1;
  --accent-200: #9A73B5;
  --text-100: #4A4A4A;
  --text-200: #878787;
  --bg-100: #F5F3F7;
  --bg-200: #E9E4ED;
  --bg-300: #FFFFFF;
}

/* 表单容器样式 */
.el-form {
  background: var(--bg-300);
  padding: 25px;
  border-radius: 12px;
  box-shadow: 0 4px 12px rgba(139, 95, 191, 0.08);
  border: 1px solid var(--bg-200);
}

/* 表单标题样式 */
.el-form-item__label {
  color: var(--primary-200);
  font-weight: 600;
  font-size: 14px;
  padding-right: 15px;
}

/* 输入框样式 */
.el-input__inner, .el-date-editor.el-input__inner {
  height: 40px;
  line-height: 40px;
  border-radius: 8px;
  border: 1px solid var(--bg-200);
  background-color: var(--bg-300);
  transition: all 0.3s ease;
  color: var(--text-100);
}

.el-input__inner:focus, .el-date-editor.el-input__inner:focus {
  border-color: var(--primary-100);
  box-shadow: 0 0 0 2px rgba(139, 95, 191, 0.2);
}

/* 日期选择器样式 */
.el-date-editor .el-input__prefix,
.el-date-editor .el-input__suffix {
  color: var(--primary-100);
}

/* 按钮样式 */
.el-button {
  border-radius: 8px;
  padding: 10px 20px;
  font-weight: 500;
  transition: all 0.3s ease;
}

.el-button--primary {
  background: linear-gradient(135deg, var(--primary-100), var(--primary-200));
  border: none;
  color: var(--primary-300);
}

.el-button--primary:hover {
  background: linear-gradient(135deg, var(--primary-200), var(--primary-100));
  transform: translateY(-2px);
  box-shadow: 0 4px 8px rgba(139, 95, 191, 0.3);
}

.el-button--default {
  border: 1px solid var(--primary-100);
  color: var(--primary-100);
  background: var(--bg-300);
}

.el-button--default:hover {
  background: var(--primary-100);
  color: var(--primary-300);
}

/* 表单项间距 */
.el-form-item {
  margin-bottom: 22px;
}

/* 搜索表单特殊样式 */
.el-form--inline .el-form-item {
  margin-right: 15px;
  margin-bottom: 15px;
}

/* 表格操作按钮样式 */
.el-button--text {
  color: var(--primary-100);
  font-weight: 500;
}

.el-button--text:hover {
  color: var(--primary-200);
}

/* 对话框表单样式 */
.el-dialog__body .el-form {
  padding: 10px 20px;
}

/* 响应式调整 */
@media (max-width: 768px) {
  .el-form--inline .el-form-item {
    display: block;
    margin-right: 0;
    width: 100%;
  }

  .el-form-item__content {
    width: 100%;
  }

  .el-input, .el-date-editor {
    width: 100% !important;
  }
}

/* 添加动画效果 */
@keyframes fadeIn {
  from { opacity: 0; transform: translateY(10px); }
  to { opacity: 1; transform: translateY(0); }
}

.el-form-item {
  animation: fadeIn 0.3s ease forwards;
}

/* 为不同表单项添加延迟动画 */
.el-form-item:nth-child(1) { animation-delay: 0.1s; }
.el-form-item:nth-child(2) { animation-delay: 0.2s; }
.el-form-item:nth-child(3) { animation-delay: 0.3s; }
.el-form-item:nth-child(4) { animation-delay: 0.4s; }
.el-form-item:nth-child(5) { animation-delay: 0.5s; }
</style>
